package com.lefu.es.application;

import android.app.Application;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.support.multidex.MultiDex;
import android.util.Log;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.lefu.es.cache.CacheHelper;
import com.lefu.es.cache.CacheHelper1;
import com.lefu.es.db.DatabaseCreatOrUpdateHelper;
import com.lefu.es.db.DatabaseCreatOrUpdateHelper1;
import com.lefu.es.db2.DaoMaster;
import com.lefu.es.db2.DaoSession;
import com.lefu.es.util.LogUtil;
import com.lefu.es.util.LogUtils;

import java.util.Locale;

/**
 * 作者: Administrator on 2016/12/13.
 * 作用:
 */

public class IwellnessApplication extends Application {  //MultiDexApplication

    public static IwellnessApplication app = null;
    public static int isShowFag = 0;
    public static long dilogDate;
    private static DaoSession daoSession;

    public static DaoSession getDaoInstant() {
        return daoSession;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        app = this;
        /*异常捕捉*/
//		CrashHandler crashHandler = CrashHandler.getInstance();
//		crashHandler.init(getApplicationContext());
        Fresco.initialize(getApplicationContext());
        /***初始化数据库***/
        setupDatabase();
        new IwellnessApplication.initAsyncTask().execute();//初始化营养参数
        LogUtil.setDebug(false);
    }

    /**
     * 配置数据库
     */
    private void setupDatabase() {
        /***创建数据库mac.db***/
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "mac.db", null);
        /***获取可写数据库***/
        SQLiteDatabase db = helper.getWritableDatabase();
        /***获取数据库对象***/
        DaoMaster daoMaster = new DaoMaster(db);
        /***获取Dao对象管理者***/
        daoSession = daoMaster.newSession();
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    /**
     * 加载基础数据
     */
    private void initCacheDates() {
        // 基础数据
        if (null == CacheHelper.nutrientList || CacheHelper.nutrientList.size() == 0) {
            SQLiteDatabase database = DatabaseCreatOrUpdateHelper.createOrUpdateDB(this);
            CacheHelper.cacheAllNutrientsDate(this, database);
            if (null != database) {
                database.close();
                database = null;
            }
        }
    }

    /**
     * 加载基础数据
     */
    private void initCacheDates1() {
        // 基础数据
        if (null == CacheHelper.nutrientList || CacheHelper1.nutrientList.size() == 0) {
            SQLiteDatabase database = DatabaseCreatOrUpdateHelper1.createOrUpdateDB(this);
            CacheHelper1.cacheAllNutrientsDate(this, database);
            if (null != database) {
                database.close();
                database = null;
            }
        }
    }

    private boolean isZh() {
        Locale locale = getResources().getConfiguration().locale;
        String language = locale.getLanguage();
        if (language.endsWith("zh"))
            return true;
        else
            return false;
    }

    class initAsyncTask extends AsyncTask<Integer, Integer, Integer> {

        @Override
        protected Integer doInBackground(Integer... arg0) {

            if (isZh()) {
                LogUtils.d("Application", "加载zh");
                initCacheDates();
            } else {
                LogUtils.d("Application", "加载en");
                initCacheDates1();
            }
            return 0;
        }

        @Override
        protected void onPreExecute() {

            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(Integer result) {
            Log.i("Application*****", "缓存初始化完成");
        }

        @Override
        protected void onProgressUpdate(Integer... values) {

        }

    }
}
